/*
 * Linklist.c
 *
 *  Created on: 2009-5-24
 *      Author: hwg
 */

#include "../header/common.h"
//*******************common***********************
Status	Listsetnode(LinkList L, int i, ElemType *e)
{
	int k=0;
	LinkList p=L;
	i = i + 1;
	if(i<1){return error;}
	while(p && k<i-1){p=p->next; k++;}
	if(k>i-1 || !p){return error;}
	p->data = *e;
	return ok;
}
Status	Listgetnode(LinkList L, int i, ElemType *e)
{
	int k=0;
	LinkList p=L;
	i = i + 1;
	if(i<1){return error;}
	while(p && k<i-1){p=p->next; k++;}
	if(k>i-1 || !p){return error;}
	*e = p->data;
	return ok;
}
Status ListInsert_L(LinkList L, int i, ElemType e)
{
	int k=0;
	LinkList p=L;
	if(i<1){return error;}
	while(p && k<i-1){p=p->next; k++;}
	if(k>i-1 || !p){return error;}
	LinkList new_node = malloc(sizeof(LNode));
	new_node->data = e;
	new_node->next = p->next;
	p->next = new_node;
	return ok;
}
Status ListInsertbutton_Sq(LinkList L, ElemType e)
{
	LinkList p=L;
	LinkList new_node = malloc(sizeof(LNode));
	new_node->data = e;
	new_node->next = p->next;
	p->next = new_node;
	return ok;
}
Status InitList_L(LinkList *L)
{
	*L = malloc(sizeof(LNode));
	(*L)->next = NULL;
	return ok;
}
//*******************key*************************for class keynode
//typedef struct keynode{
//	char words[CHARBUFFERSIZE];
//	char exp[EXPBUFFSIZE];
//	struct keynode * next;
//}keynode;

Status initlist_keylist(LinkList *L , char *string)
{
	*L = malloc(sizeof(LNode));
	(*L)->next = NULL;
	LinkList p = *L;
	buff bu;
	initbuff(&bu , string);
	int i = 0;
	while(i != error)
	{
		 LinkList new_node = malloc(sizeof(LNode));
		 keynode knode;
		 i = getkeynode(&bu ,&knode);
		 if(i == error)
			 return ok;
		 new_node->data.unikeynode = knode;
		 new_node->next = NULL;
		 p->next = new_node;
		 p = new_node;
	}
	return ok;
}
void printf_keylist(LinkList L)
{
	LinkList p = L;
	p = p->next;
	while(p != NULL)
	{
		printf("\nthe key node.words =%s\tthe node.exp=%s\n",p->data.unikeynode.words,p->data.unikeynode.exp);
		p = p->next;
	}
}
//*******************word*************************for class word
//typedef struct wordnode{
//	char words[CHARBUFFERSIZE];
//	int order;
//	char exp[EXPBUFFSIZE];
//	char value[10];
//	struct wordnode * next;
//}wordnode;
int initlist_wordlist(LinkList *L , char *string)
{
	*L = malloc(sizeof(LNode));
	(*L)->next = NULL;
	LinkList p = *L;
	buff bu;
	initbuff(&bu , string);
	int i = 0;
	while(i != error)
	{
		 LinkList new_node = malloc(sizeof(LNode));
		 wordnode wnode;
		 i = getwordsnode(&bu ,&wnode);
		 if(i == error)
			 return ok;
		 wnode.exp[0] = '\0';
		 new_node->data.uniwordnode = wnode;
		 new_node->next = NULL;
		 p->next = new_node;
		 p = new_node;
	}
	return ok;
}

void printf_wordlist(LinkList L)
{
	LinkList p = L;
	p = p->next;
	printf("\n\tWords\torder\texp\n");
	while(p != NULL)
	{
		printf("\n\t%s\t%d\t%s\n",p->data.uniwordnode.words,p->data.uniwordnode.order,p->data.uniwordnode.exp);
		p = p->next;
	}

}
Status	getWord_Sq(LinkList L, int i, ElemType *e)
{
	LinkList p;
	if(L->next != NULL)
		p =L->next;
	else
		return error;
	while(p != NULL)
	{
		if(p->data.uniwordnode.order == i)
		{
			*e = p->data;
			return ok;
		}

		else
		{
			p=p->next;
		}

	}
	return error;
}
//*******************Action*****************for class elmAction
//struct{ char identyfy;int action;int number;}elmAction


Status	searchAction_Sq(LinkList L, char iden ,ElemType *e)
{
	LinkList p;
	if(L->next != NULL)
		p =L->next;
	else
		return error;
	while(p != NULL)
	{
		if(p->data.uniaction.identyfy == iden)
		{
			*e = p->data;
			return ok;
		}

		else
		{
			p=p->next;
		}

	}
	return error;
}
//*******************grammarexp*********************for class grammarexp
int initlist_gramexplist(LinkList *L , char *string)
{
	*L = malloc(sizeof(LNode));
	(*L)->next = NULL;
	LinkList p = *L;
	buff bu;
	initbuff(&bu , string);
	int i = 0;
	while(i != error)
	{
		 LinkList new_node = malloc(sizeof(LNode));
		 productnode Pnode;
		 i = getgramexp(&bu ,&Pnode);
		 if(i == error)
			 return ok;
		 new_node->data.uniproduction = Pnode;
		 new_node->next = NULL;
		 p->next = new_node;
		 p = new_node;
	}
	return ok;
}
void printf_gramexplist(LinkList L)
{
	LinkList p = L;
	p = p->next;
	printf("\nindex\theader\texp\tmidle code\n");
	while(p != NULL)
	{
		printf("\n%d\t%c\t%s\t=%s\n",p->data.uniproduction.index,p->data.uniproduction.h,p->data.uniproduction.express,p->data.uniproduction.middlecode);
		p = p->next;
	}

}
